package com.ruoyi.web.controller.app;


import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.MD5Util;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.order.service.INewProductService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/app")
public class LoginAppController {

    @Resource
    private SysUserMapper sysUserMapper ;

    @RequestMapping(value = "/login",method ={ })
    @ResponseBody
    public String login(Integer current, Integer size){
        return "true";
    }

    /**
     * 校验验证码
     */
    @RequestMapping(value = "/user/checkRegisterSms" ,method = RequestMethod.PUT)
    @ResponseBody
    public String   checkRegisterSms(String mobile, String validCode){
        if (!"888888".equals(validCode)){
            return "验证码有误或已过期";
        }
        else {
            return "";
        }
    }
    /**
     * 发送验证码
     */
    @RequestMapping(value = "/user/sendRegisterSms" ,method = RequestMethod.PUT)
    @ResponseBody
    public String   checkRegisterSms(String mobile){
        return "";
    }

    /**
     * {"appType":4,"checkRegisterSmsFlag":"f756db289dcb4679a7225018f8fd6d4b",
     * "mobile":"18476850305",
     * "password":"bo78Q99QSXcqAq+NqF2IxJdz3cY3gHiyeuFaJujJZLY=",
     * "validCode":"131296","validateType":1}
     */
    /**
     * 修改密码
     */
    @RequestMapping(value = "/user/updatePwd" ,method = RequestMethod.PUT)
    @ResponseBody
    public String   updatePwd(String appType , String mobile , String passwrod,String origPassword, String validCode , String  validateType ){
        if (!StringUtils.isEmpty(mobile)){
            SysUser sysUser  =  sysUserMapper.selectUserByPhoneNumber(mobile);
            if (sysUser == null ){
                return "不存在手机号";
            }else {
                sysUser = new SysUser();
                sysUser.setUserId(sysUser.getUserId());
                sysUser.setOrig_password(origPassword);
                sysUserMapper.updateUser(sysUser);
            }
        }
        return "修改成功";
    }
    /**
     * 登录
     */
    @RequestMapping(value = "/tempSocialLogin" ,method = RequestMethod.POST)
    @ResponseBody
    public Object   tempSocialLogin(HttpServletRequest request  ,  String userName , String passWord , String origPassword , String tempUid  ){
        SysUser sysUser  =  sysUserMapper.selectUserByPhoneNumber(userName);
        if (sysUser !=null){
            //登录成功
            if (sysUser.getOrig_password().equals(origPassword)){
                String token =  MD5Util.getSaltMD5(origPassword);
                 HttpSession session  =  request.getSession();
                 sysUser.setToken(token);
                 session.setAttribute("loginUser" , sysUser);
                 Map<String,String > map = new HashMap<>();
                 map.put("expiresIn" ,"2592000");
                 map.put("accessToken" , token);
                 map.put("refreshToken", token);
                return map ;
            }
            else{
                return "账号或密码不正确";
            }
        }
        return "" ;
    }

    @RequestMapping(value = "/p/user/getUserInfo" ,method = RequestMethod.GET)
    @ResponseBody
    public String getUserInfo(){
        return "{\"couponNum\":0,\"score\":100,\"notifyNum\":0,\"totalBalance\":0.0}\n";
    }

    @RequestMapping(value = "/p/user/centerInfo" ,method = RequestMethod.GET)
    @ResponseBody
    public String centerInfo(){
        return "{\"isSetPassword\":null,\"shopAuditStatus\":null,\"orderCountData\":{\"confirm\":0,\"success\":0,\"consignment\":0,\"allCount\":6,\"unPay\":0,\"close\":6,\"payed\":0,\"refund\":0},\"shopId\":null}";
    }
    @RequestMapping(value = "/p/user/userInfo" ,method = RequestMethod.GET)
    @ResponseBody
    public String userInfo(){
        return "{\"level\":1,\"nickName\":\"测试\",\"sex\":null,\"mobile\":\"184****0305\",\"pic\":null,\"userId\":\"2e99177d5ebc451488060fd9cac3846b\",\"birthDate\":null,\"token\":null,\"score\":100,\"balance\":0.0,\"userMobile\":\"18476850305\",\"levelType\":0,\"growth\":0,\"status\":1}";
    }
}
